package dreamCookGame;

import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class Item {

	public String _name;
	public BufferedImage _img;
	String[] _stateImgNames;
	ItemMenu _itemMenu;
	public int _x;
	public int _y;
	Boolean _menuOn;
	String[] _menuOptions;
	public Button _itemButton;
	String _shape;
	boolean _isInInventory;
	AudioClipBig _audioClip;
	String _audioClipName;
	ItemCapacity _capacity;
	Boolean _hasCapacity;
	ItemBehavior _behavior;
	String[] _behaviorStateImgNames;

	public Item(String name, String[] stateImgNames, int x, int y, String[] menuOptions, String shape, String audioClipName, Boolean hasCapacity, String[] behaviorStateImgNames) {
		_name = name;
		_stateImgNames = stateImgNames;
		_x = x;
		_y = y;
		_menuOn = false;
		_menuOptions = menuOptions;
		_shape = shape;
		_isInInventory = false;
		_audioClipName = audioClipName;
		_behaviorStateImgNames = behaviorStateImgNames;
		
		if(hasCapacity){
			_capacity = new ItemCapacity();
			_hasCapacity = true;
		}		
		else
			_hasCapacity = false;		
	}
	
	public void createItemMenu(){				
		//TODO: Make item menu position dependent on button image (Brittany)
		if(_menuOptions != null)
			_itemMenu = new ItemMenu(_x+_img.getWidth()/2, _y+_img.getHeight()/2, _menuOptions);
	}
	 
	public void loadResources(References reference) {
		
		_img = reference.getImageArray(_stateImgNames[0])[0];
		
		createButton();

		_itemButton.loadResources(reference);
		
		if(_menuOptions != null) {
			createItemMenu();
			_itemMenu.loadResources(reference);
		}
		
		if(_behaviorStateImgNames != null) {		
			_behavior = new ItemBehavior(_x, _y, _behaviorStateImgNames);
			_behavior.loadResources(reference);
		}
		
		if(_audioClipName != null) {
			_audioClip = new AudioClipBig(_audioClipName);
			_audioClip.loadResouces(reference);
		}
	}

	public void updatePosition(int x, int y) {
		_x = x;
		_y = y;
		_itemButton.updatePositionRectangle(x, y);//creates a rectangular bounding box
		updateItemButtonBoundingBox();//corrects the previous step and creates the correctly shaped bounding box
	}
	
	public boolean isWithinItem(int x, int y) {
		int leftBound = _x;
		int rightBound = _x + _img.getWidth();
		int topBound = _y;
		int bottomBound = _y + _img.getHeight();
		
		if(x >= leftBound && x <= rightBound && y >= topBound && y <= bottomBound)
			return true;
		
		return false;
	}
	
	public void createButton(){
		if(_shape == null){
			Rectangle boundingBox = new Rectangle(_x, _y, _img.getWidth(), _img.getHeight());
			_itemButton = new Button(_x, _y, boundingBox, _stateImgNames);	
		}
		
		else if(_shape == "triangleUp"){
			int[] xPoints = {_x, (_x+_img.getWidth()/2), (_x+_img.getWidth())};
			int[] yPoints = {_y+_img.getHeight(), _y, _y+_img.getHeight()};
			Polygon boundingBox = new Polygon(xPoints, yPoints, 3);
			_itemButton = new Button(_x, _y, boundingBox, _stateImgNames);	
		}
		
		else if(_shape == "triangleDown"){
			int[] xPoints = {_x, (_x+_img.getWidth()/2), (_x+_img.getWidth())};
			int[] yPoints = {_y, _y+_img.getHeight(), _y};
			Polygon boundingBox = new Polygon(xPoints, yPoints, 3);
			_itemButton = new Button(_x, _y, boundingBox, _stateImgNames);	
		}
	}
	
	public void drawMenu(){
		_menuOn = true;
	}
	
	public void undrawMenu(){
		_menuOn = false;
	}

	public void shift(int dx, int dy) {
		_x+=dx;
		_y+=dy;
		_itemButton.shift(dx,dy);
		updateItemButtonBoundingBox();
		_itemMenu.shift(dx,dy);
	}

	private void updateItemButtonBoundingBox() {
		if(_shape == null){
			Rectangle boundingBox = new Rectangle(_x, _y, _img.getWidth(), _img.getHeight());
			_itemButton._boundingBox = boundingBox;
		}
		
		else if(_shape == "triangleUp"){
			int[] xPoints = {_x, (_x+_img.getWidth()/2), (_x+_img.getWidth())};
			int[] yPoints = {_y+_img.getHeight(), _y, _y+_img.getHeight()};
			Polygon boundingBox = new Polygon(xPoints, yPoints, 3);
			_itemButton._boundingBox = boundingBox;
		}
		
		else if(_shape == "triangleDown"){
			int[] xPoints = {_x, (_x+_img.getWidth()/2), (_x+_img.getWidth())};
			int[] yPoints = {_y, _y+_img.getHeight(), _y};
			Polygon boundingBox = new Polygon(xPoints, yPoints, 3);
			_itemButton._boundingBox = boundingBox;
		}
	}	
}
